package com.chushouya.controller.product.admin;

import com.chushouya.common.constant.ServiceType;
import com.chushouya.product.dto.admin.attribute.*;
import com.general.framework.core.enums.BusinessType;
import com.general.framework.core.schema.PageResult;
import com.general.framework.core.schema.Response;
import com.general.framework.web.annotations.Log;
import com.general.framework.web.annotations.RepeatSubmit;
import com.chushouya.product.service.admin.AttributeItemService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Api(tags = ServiceType.PLATFORM + "属性项目管理")
@RestController
@RequestMapping(ServiceType.PLATFORM + "/attributeItem")
public class AttributeItemController {

    @Resource
    private AttributeItemService attributeItemService;

    @ApiOperation("获取属性项目分页列表")
    @GetMapping("/page")
    public Response<PageResult<AttributeItemListDTO>> page(AttributeItemQuery query) {
        return Response.success(attributeItemService.selectAttributeItemPageResult(query));
    }

    @ApiOperation("获取属性项目列表")
    @GetMapping("/list")
    public Response<List<AttributeItemListDTO>> list(AttributeItemQuery query) {
        return Response.success(attributeItemService.selectAttributeItemList(query));
    }

    @ApiOperation("根据ID获取属性项目详细信息")
    @GetMapping("/detail")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "attributeItemId", value = "属性项目ID", required = true),
    })
    public Response<AttributeItemDetailDTO> getDetail(Long attributeItemId) {
        return Response.success(attributeItemService.getAttributeItemDetail(attributeItemId));
    }

    @ApiOperation("新增属性项目")
    @Log(businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @RepeatSubmit
    public Response<Long> add(@Validated @RequestBody AttributeItemSaveDTO attributeItem) {
        return Response.success(attributeItemService.addAttributeItem(attributeItem));
    }

    @ApiOperation("修改属性项目")
    @Log(businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    public Response<Long> edit(@Validated @RequestBody AttributeItemUpdateDTO attributeItem) {
        return Response.success(attributeItemService.editAttributeItem(attributeItem));
    }

    @ApiOperation("删除属性项目")
    @Log(businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "attributeItemId", value = "属性项目ID", required = true)
    })
    public Response<Void> remove(Long attributeItemId) {
        attributeItemService.removeAttributeItem(attributeItemId);
        return Response.success();
    }

    @ApiOperation("根据分类ID获取属性项目列表")
    @GetMapping("/listByCategoryId")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "categoryId", value = "分类ID", required = true)
    })
    public Response<List<AttributeItemListDTO>> listByCategoryId(Long categoryId) {
        return Response.success(attributeItemService.selectAttributeItemListByCategoryId(categoryId));
    }
}
